package servlet.user;

import dao.UserDao;
import entity.User;
import util.Empty;
import util.R;
import util.ResposeUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    UserDao userDao = new UserDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        if (Empty.isEmpty(userName)||Empty.isEmpty(password)) {
            ResposeUtil.write(R.data(400,null,"密码不能为空"),resp);
        }else {
            User user = userDao.getOne("user_name", userName);
            String msg = "";
            Integer code = 400;
            User data = null;
            if (Empty.isEmpty(user)){
                msg = "用户不存在";
            } else {
                if (!password.equals(user.getPassword())){
                    msg = "密码错误";
                } else {
                    code = R.CODE_SUCCESS;
                    user.setPassword(null);
                    data = user;
                }
            }
            System.out.println(data);
            ResposeUtil.write(R.data(code,data,msg),resp);
        }
    }

}
